import { darkMode, themeState } from '~/types/theme'
import { setStorage, getStorage, removeStorage } from '@/utils/storage'

const initDarkMode = (getStorage('DARK_MODE') as darkMode) || darkMode.LIGHT
const initPrimary = (getStorage('PRIMARY_COLOR') as string) || '#3c7eff'

export const useTheme = defineStore({
  id: 'theme',
  state: (): themeState => ({
    primary: initPrimary,
    darkMode: initDarkMode
  }),
  actions: {
    //设置主题颜色
    setPrimary(color: string) {
      this.primary = color
      setStorage('PRIMARY_COLOR', color)
    },
    setDarkMode(type: darkMode) {
      this.darkMode = type
      setStorage('DARK_MODE', type)
    },
    // 重置主题
    resetTheme() {
      this.setPrimary('#10aec2')
      this.setDarkMode(darkMode.LIGHT)
    },
    // 清除缓存
    clearThemeStorage() {
      removeStorage('PRIMARY_COLOR')
      removeStorage('DARK_MODE')
    }
  }
})
